package com.byt.framlib.commonutils;

import java.util.List;

/**
 * Web相关工具
 */
public class WebUtil {
    /**
     * 富文本
     *
     * @param bodyHTML
     * @return
     */
    public static String getH5Data(String bodyHTML) {
        bodyHTML = bodyHTML.replace("<img", "<img style='max-width:100%; width:auto;height:auto;'");
        String head = "<head><meta http-equiv=\"pragma\" content=\"no-cache\">" + "</head>";
        return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
    }

    /**
     * "
     * 富文本
     *
     * @param bodyHTML
     * @return
     */
    public static String getHtmlData(String bodyHTML) {
        String head = "<head>"
                + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> "
                + "<style>img{max-width: 100%; width:auto; height:auto;}</style>"
                + "</head>";
        return "<html>" + head + "<body>" + bodyHTML + "</body></html>";
    }


    //css样式,隐藏header
    private static String HIDE_HEADER_STYLE = "<style>div.headline{display:none;}</style>";
    //css style tag,需要格式化
    private static String NEEDED_FORMAT_CSS_TAG = "<link rel=\"stylesheet\" type=\"text/css\" href=\"%s\"/>";
    // js script tag,需要格式化
    private static String NEEDED_FORMAT_JS_TAG = "<script src=\"%s\"></script>";
    public static String MIME_TYPE = "text/html; charset=utf-8";
    public static String ENCODING = "utf-8";

    /**
     * 根据css链接生成Link标签
     *
     * @param url String
     * @return String
     */
    public String createCssTag(String url) {
        return String.format(NEEDED_FORMAT_CSS_TAG, url);
    }

    public String createCssTag(List<String> urls) {
        StringBuffer sb = new StringBuffer();
        for (String url : urls) {
            sb.append(createCssTag(url));
        }
        return sb.toString();
    }

    public String createJsTag(String url) {
        return String.format(NEEDED_FORMAT_JS_TAG, url);
    }

    public String createJsTag(List<String> urls) {
        StringBuffer sb = new StringBuffer();
        for (String url : urls) {
            sb.append(createJsTag(url));
        }
        return sb.toString();
    }

    public String createHtmlData(String html, List<String> cssList, List<String> jsList) {
        String css = createCssTag(cssList);
        String js = createJsTag(jsList);
        return "$css$HIDE_HEADER_STYLE$html$js";
    }

}
